package com.gpjcjl.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;

import com.base.lib.ToastUtil;
import com.gpjcjl.BaseActivity;
import com.gpjcjl.R;
import com.gpjcjl.adapter.RecordDetailAdapter;
import com.gpjcjl.entity.Record;
import com.gpjcjl.entity.RecordDetail;
import com.gpjcjl.event.RecordDetailUpdate;
import com.yanzhenjie.recyclerview.OnItemClickListener;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 原始记录详情页（气瓶列表页）
 */
public class RecordDetailListActivity extends BaseActivity implements OnItemClickListener, RecordDetailAdapter.OnDeleteListener {

    @BindView(R.id.listView)
    SwipeRecyclerView listView;
    @BindView(R.id.edit)
    EditText editText;
    private RecordDetailAdapter adapter;

    private Record record;
    private boolean isRemote;
    private RecordDetailViewModel viewModel;

    @Override
    protected int getContentView() {
        return R.layout.activity_record_detail_list;
    }

    public static void startIntent(Context context, Record record, boolean isRemote) {
        Intent it = new Intent(context, RecordDetailListActivity.class);
        it.putExtra("record", record);
        it.putExtra("isRemote", isRemote);
        context.startActivity(it);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(RecordDetailUpdate event) {
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        viewModel = new ViewModelProvider(this).get(RecordDetailViewModel.class);

        DividerItemDecoration divider = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        divider.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(mContext, R.drawable.item_divider8)));
        listView.addItemDecoration(divider);

        record = (Record) getIntent().getExtras().get("record");
        isRemote = getIntent().getBooleanExtra("isRemote", false);

        listView.setOnItemClickListener(this);

        listView.setAdapter(adapter = new RecordDetailAdapter(mContext, new ArrayList<>()));
        adapter.setOnDeleteListener(this);

        initData();
    }

    private void initData() {
        showLoading();
        viewModel.recordDetailList(isRemote, record).observe(this, result -> {
            dismissLoading();
            if (result.isSuccess()) {
                adapter.addList(result.getData(), true);
            } else {
                ToastUtil.showToast(mContext, result.getMsg());
            }
        });

    }

    @Override
    public void onItemClick(View view, int position) {
        RecordDetail recordDetail = adapter.getItem(position);
        RecordDetailEditActivity.startIntent(mContext, recordDetail, isRemote);
    }

    @OnClick({R.id.btn_add, R.id.btn_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_add:
                viewModel.createRecordDetail(record, adapter.getDatas()).observe(this, result -> {
                    if (result.isSuccess()) {
                        RecordDetailAddActivity.startIntent(mContext, result.getData(), isRemote);
                    }
                });
                break;
            case R.id.btn_search:
                String keyword = editText.getText().toString().trim();
                showLoading();
                viewModel.searchList(isRemote, record.getId(), keyword).observe(this, result -> {
                    dismissLoading();
                    if (result.isSuccess()) {
                        adapter.addList(result.getData(), true);
                    } else {
                        ToastUtil.showToast(mContext, result.getMsg());
                    }
                });
                break;
            default:
                break;
        }
    }

    @Override
    public void onDelete(int position) {
        showLoading();
        viewModel.deleteRecordDetail(isRemote, adapter.getItem(position)).observe(this, result -> {
            dismissLoading();
            ToastUtil.showToast(getApplication(), result.getMsg());
            if (result.isSuccess()) {
                EventBus.getDefault().post(new RecordDetailUpdate());
            }

        });
    }
}
